﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Model;

namespace ConAppCore.Test
{
    internal class TestExpireFileCleaner
    {
        public static void Test()
        {
            //Test1();
            Test2();
        }

        private static void Test2()
        {
            var expireFileCleaner = new ExpireFileCleaner(1000);
            expireFileCleaner.OnDeleteFailed += ExpireFileCleaner_OnDeleteFailed;
            expireFileCleaner.Add(new ExpireFileCleanerItem(@"D:\HBData", 2, 100, GetFileCheckTimeCallback2));
            expireFileCleaner.Start();

            Console.WriteLine("any key exit..");
            Console.ReadKey();
        }

        private static DateTime? GetFileCheckTimeCallback2(FileInfo fileInfo)
        {
            DateTime? createTime = null;
            TryGetFileCreateTime(fileInfo.Name, out createTime);
            return createTime;
        }

        /// <summary>
        /// 尝试获取文件创建时间,获取成功返回true,失败返回false
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="createTime">创建时间</param>
        /// <returns>获取成功返回true,失败返回false</returns>
        private static bool TryGetFileCreateTime(string fileName, out DateTime? createTime)
        {
            createTime = null;

            try
            {
                if (fileName.Contains(PathEx.LeftSlash) || fileName.Contains(PathEx.RightSlash))
                {
                    fileName = Path.GetFileNameWithoutExtension(fileName);
                }

                //20240306085513_HY02BJWD50371004_F_0095500000_B_0000200000_S_0000008000.wav
                int year, month, day, hours, minuts, seconds;

                if (!int.TryParse(fileName.Substring(0, 4), out year))
                {
                    return false;
                }

                if (!int.TryParse(fileName.Substring(4, 2), out month))
                {
                    return false;
                }

                if (!int.TryParse(fileName.Substring(6, 2), out day))
                {
                    return false;
                }

                if (!int.TryParse(fileName.Substring(8, 2), out hours))
                {
                    return false;
                }

                if (!int.TryParse(fileName.Substring(10, 2), out minuts))
                {
                    return false;
                }

                if (!int.TryParse(fileName.Substring(12, 2), out seconds))
                {
                    return false;
                }

                createTime = new DateTime(year, month, day, hours, minuts, seconds);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private static void ExpireFileCleaner_OnDeleteFailed(object sender, EventArgs<IOException> e)
        {
            //删除过期文件失败异常,不输出日志
        }


        private static void Test1()
        {
            var expireDays = 1;
            var availableSpaceSize = 100;
            var expireFileCleaner = new ExpireFileCleaner(1000);
            //expireFileCleaner.Add(new ExpireFileCleanerItem(@"D:\MetaData\MN", expireDays, availableSpaceSize, GetFileCheckTimeCallback));
            expireFileCleaner.Add(new ExpireFileCleanerItem(@"D:\MetaData\MN", expireDays, availableSpaceSize, null));
            expireFileCleaner.Start();

            Console.WriteLine("any key exit..");
            Console.ReadKey();
        }


        private static DateTime? GetFileCheckTimeCallback(FileInfo fileInfo)
        {
            try
            {
                //20240306085513_HY02BJWD50371004_F_0095500000_B_0000200000_S_0000008000.wav
                int year = int.Parse(fileInfo.Name.Substring(0, 4));
                int month = int.Parse(fileInfo.Name.Substring(4, 2));
                int day = int.Parse(fileInfo.Name.Substring(6, 2));

                int hours = int.Parse(fileInfo.Name.Substring(8, 2));
                int minuts = int.Parse(fileInfo.Name.Substring(10, 2));
                int seconds = int.Parse(fileInfo.Name.Substring(12, 2));

                return new DateTime(year, month, day, hours, minuts, seconds);
            }
            catch
            {
                return null;
            }
        }



    }
}
